using System;
using System.Collections.Generic;
using System.Text;
using SlimDX;

namespace miciv
{
    public class TemplateList : Frame
    {
        protected Template m_template;
        protected int m_sFirstVisibleIndex;

        public TemplateList(CoreContext _coreContext, string _strID)
            : base(_coreContext, _strID)
        {
        }

        public TemplateList(CoreContext _coreContext, string _strID, Frame _frmParent)
            : base(_coreContext, _strID, _frmParent)
        {
        }

        public Template Template
        {
            get
            {
                return this.m_template;
            }
            set
            {
                if (value != this.m_template)
                {
                    this.m_template = value;
                    this.CreateChildren();
                }
            }
        }

        public int FirstVisibleIndex
        {
            get
            {
                return this.m_sFirstVisibleIndex;
            }
        }

        override protected void InternalInitialize(CoreContext _coreContext, string _strID)
        {
            base.InternalInitialize(_coreContext, _strID);
            this.m_template = null;
            this.m_sFirstVisibleIndex = 0;
        }

        override public Frame Clone(Frame _frmParent, string _strID)
        {
            Frame frame = new TemplateList(this.m_coreContext, this.m_strID + _strID, _frmParent);
            frame.Copy(this);
            // Unlike other derived frame classes : DO NOT clone children !! they will be recreated throught CreateChildren at approriate time.
            //foreach (Frame child in this.m_lstChildren)
            //{
            //    child.Clone(frame);
            //}
            return frame;
        }

        override public void Copy(Frame _frame)
        {
            base.Copy(_frame);
            TemplateList tl = _frame as TemplateList;
            this.Template = tl.Template;
            this.m_sFirstVisibleIndex = tl.FirstVisibleIndex;
        }

        override public Frame Clone(Frame _frmParent)
        {
            return this.Clone(_frmParent, "");
        }

        override public void UpdateData(float _deltaTime)
        {
            base.UpdateData(_deltaTime);
            if ((false != this.m_bVisible) && (null != this.DataSource))
            {
                int sIndex = this.m_sFirstVisibleIndex;
                foreach (Frame frame in this.m_lstChildren)
                {
                    frame.Visible = (this.DataSource.Count > sIndex);
                    frame.DataSourceIndex = sIndex;
                    ++sIndex;
                }
                this.m_bDirty = false;
            }
        }

        override public bool NeedLayoutUpdate
        {
            get
            {
                return ((false != base.NeedLayoutUpdate) && (null != this.Template));
            }
        }

        public override void SetLayout(int _sRows, int _sColumns, bool _bFeedRowFirst)
        {
            base.SetLayout(_sRows, _sColumns, _bFeedRowFirst);
            this.CreateChildren();
        }

        protected void CreateChildren()
        {
            this.ReleaseAllChildren();
            if ((null != this.Template) && (null != this.LayoutProcessor))
            {
                for (int j = 0; this.LayoutProcessor.Rows > j; ++j)
                {
                    for (int i = 0; this.LayoutProcessor.Columns > i; ++i)
                    {
                        this.Template.Clone(this, string.Format("_c{0}r{1}", i, j));
                    }
                }
            }
        }
    }
}
